package com.ankicoo.common.datainit;

import org.apache.ibatis.jdbc.ScriptRunner;

import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * description:  address info init
 *
 * @author : qcyang
 * @date :  2021/7/7 9:59
 */
public class AddressInit {
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //数据库地址
    private static final String URL = "jdbc:mysql://rm-bp1l3y6l6987449522o.mysql.rds.aliyuncs.com:3306/ankicoo?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8";
    private static final String USERNAME = "qcyang";
    private static final String PASSWORD = "Yang@120512";
    //脚本文件 见 resources
    private static final File FILE = new File("D://AddressSql_1.sql");


    /**
     * 执行sql脚本文件 使用Spring工具类
     */
    private static void mybatisExec() throws ClassNotFoundException, SQLException {
        Class.forName(DRIVER);
        Connection conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        ScriptRunner runner = new ScriptRunner(conn);
        try {
            runner.setStopOnError(true);
            runner.setSendFullScript(true);
            FileReader reader = new FileReader(FILE);
            runner.runScript(reader);
        } catch (Exception e) {
            e.printStackTrace();
        }
        conn.close();
    }

    public void main(String[] args) throws SQLException, ClassNotFoundException{
        mybatisExec();
    }
}
